<template>
  <div class="login">
    <vue-particles
      class="login-bg"
      color="#39AFFD"
      :particleOpacity="0.7"
      :particlesNumber="100"
      shapeType="circle"
      :particleSize="4"
      linesColor="#8DD1FE"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    >
    </vue-particles>
    <!--    <div class="left_img">-->
    <!--      <img style="width:100%;height: 100%" src="http://ryspnyhk0.hn-bkt.clouddn.com/CachedImage_1920_1080_POS4.jpg">-->
    <!--    </div>-->
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <div style="display: flex">
        <div @click="changeLoginWay(1)" class="title">密码登录</div>
        <div @click="changeLoginWay(2)" class="title">免密登录</div>
      </div>
      <div v-if="loginWay===1">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="color:black;margin:0 0 25px 0;">记住密码</el-checkbox>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'"><span style="color: black;font-size: 14px">立即注册</span>
          </router-link>
        </div>
      </div>
      <div v-if="loginWay===2">
        <el-form-item prop="mobile">
          <el-input
            v-model="loginForm.mobile"
            type="text"
            auto-complete="off"
            placeholder="手机号"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="smsCode" v-if="captchaEnabled">
          <el-input
            v-model="loginForm.smsCode"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
          </el-input>
          <div class="login-code">
            <el-button style="background-color: rgb(62,62,62);color: white" @click="getSmsCode">
              {{ computeTime > 0 ? `(${computeTime}s)已发送` : '获取验证码' }}
            </el-button>
          </div>
        </el-form-item>
      </div>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span style="color: white" v-if="!loading">登 录</span>
          <span style="color: white" v-else>登 录 中...</span>
        </el-button>
        <!--  第三方应用登录 -->
        <div style="color: black;text-align: center;margin: 2% 0"> 其他方式登录</div>
        <el-form-item style="width:100%;">
          <div class="oauth-login" style="display:flex;justify-content: space-evenly">
            <img @click="doSocialLogin('gitee')" style="width: 10%" :src="require('@/assets/images/gitee.png')">
            <img @click="doSocialLogin('qq')" style="width: 10%" :src="require('@/assets/images/QQ.png')">
            <img @click="doSocialLogin('wechat_open')" style="width: 10%" :src="require('@/assets/images/微信.png')">
            <img @click="doSocialLogin('github')" style="width: 10%" :src="require('@/assets/images/GitHub.png')">
          </div>
        </el-form-item>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {getCodeImg, getSmsCode} from "@/api/login";
import Cookies from "js-cookie";
import {encrypt, decrypt} from '@/utils/jsencrypt'
import {authBinding} from "@/api/system/auth";

export default {
  name: "Login",
  data() {
    return {
      computeTime: 0,
      // 1 密码登录  2 手机号登录
      loginWay: 1,
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        smsCode: "",
        mobile: "",
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: "",
        sms_uuid: "",
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入您的账号"}
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
        code: [{required: true, trigger: "change", message: "请输入验证码"}],
        smsCode: [{required: true, trigger: "change", message: "请输入短信验证码"}]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    doSocialLogin(source) {
      authBinding(source).then(res => {
        top.location.href = res.msg;
      });
    },
    getSmsCode() {
      if (!this.computeTime) {
        getSmsCode(this.loginForm.mobile).then(res => {
          if (res.code === 200) {
            this.$message({
              message: '验证码已发送',
              type: 'success'
            });
            this.loginForm.sms_uuid = res.uuid;
            this.computeTime = 60;
            this.timer = setInterval(() => {
              this.computeTime--;
              if (this.computeTime <= 0) {
                clearInterval(this.timer)
              }
            }, 1000);
          }

        })
      }
    },
    changeLoginWay(type) {
      this.loginWay = type
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      if (this.loginWay === 2) {
        this.$refs["loginForm"].validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("mobile", this.loginForm.mobile, {expires: 30});
              Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
            } else {
              Cookies.remove("mobile");
              Cookies.remove('rememberMe');
            }
            this.$store.dispatch("SmsLogin", this.loginForm).then(() => {
              this.$router.push({path: this.redirect || "/"}).catch(() => {
              });
            }).catch(() => {
              this.loading = false;
            });
          }


        });


      } else {
        this.$refs["loginForm"].validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, {expires: 30});
              Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
              Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
            this.$store.dispatch("Login", this.loginForm).then(() => {
              this.$router.push({path: this.redirect || "/"}).catch(() => {
              });
            }).catch(() => {
              this.loading = false;
              this.getCode();
            });
          }

        });

      }
    }
  }
};
</script>

<style scoped rel="stylesheet/scss" lang="scss">
::v-deep .login-form .el-input input {
  height: 38px;
  background: transparent;
  color: black;
}

.login {
  width: 100%;
  height: 100%;
}

.title {
  cursor: pointer;
  margin: 0px auto 30px auto;
  text-align: center;
  color: black;
}

.left_img {
  position: absolute;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
  border-radius: 6px;
  background: rgb(62, 62, 62);
  left: 5%;
  top: 15%;
  width: 60%;
  height: 80%;
  padding: 5px;
}

.login-form {
  position: absolute;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
  border-radius: 6px;
  background: white;
  width: 400px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  //height: 70%;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      color: black;
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: black;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: black;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

// 如果屎全屏展示
.login-bg {
  width: 100%;
  height: 100%;
  background: rgb(238, 238, 238);
}

.oauth-login {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.oauth-login-item {
  display: flex;
  align-items: center;
  margin-right: 10px;
}

.oauth-login-item img {
  height: 25px;
  width: 25px;
}

.oauth-login-item span:hover {
  text-decoration: underline red;
  color: red;
}
</style>
